<template>
	<view class="detail">
		<view class="image" style="text-align:center">
			<image :src="shopdetail.cover_url" mode="heightFix"></image>
		</view>
		<view class="title" v-if="shopdetail.title">
			{{shopdetail.title}}
		</view>
		<view class="center">
			<view class="money">
				{{shopdetail.price?"￥"+shopdetail.price:""}}
			</view>
			<view class="sales">
				{{shopdetail.sales?"销量："+shopdetail.sales:''}}
			</view>
		</view>
		<view class="tabs">
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>
		<view v-if="current==0">
			<u-parse :html="shopdetail.details"></u-parse>
		</view>
		<view v-if="current==1">
			<view class="comment" v-for="(res, index) in commentLists" :key="res.id">
				<view class="left">
					<image :src="res.user.avatar_url"></image>
				</view>
				<view class="right">
					<view class="top">
						<view class="name">{{ res.user.name }}</view>
					</view>
					<view class="content">{{ res.content }}</view>
					<view class="date">
						{{res.created_at}}
					</view>
				</view>
			</view>
		</view>
		<view v-if="current==2">
			<view class="shopList">
				<u-row gutter="16">
					<u-col span="6" v-for="(item,index) in likegoods" :key='index'>
						<navigator class="goods-item" :url="'/pages/cate/detail?id='+item.id">
							<u-image width="100%" height="300rpx" :src="item.cover_url"></u-image>
							<view class="title u-line-1">
								{{item.title}}
							</view>
							<view class="u-flex u-row-between">
								<view class="price">
									￥{{item.price}}
								</view>
								<view class="sales">
									销量：{{item.sales}}
								</view>
							</view>
						</navigator>
					</u-col>
				</u-row>
			</view>
		</view>
		<view class="navigation">
			<view class="left">
				<view class="item" @click="star">
					<u-icon name="star" :size="40" :color='collect?"red":""'></u-icon>
					<view class="text u-line-1">{{collect?'已收藏':'收藏'}}</view>
				</view>
				<view class="item car" @click="toCart">
					<u-badge class="car-num" :count="count" type="error" :offset="[-3, -6]"></u-badge>
					<u-icon name="shopping-cart" :size="40" :color="$u.color['contentColor']"></u-icon>
					<view class="text u-line-1">购物车</view>
				</view>
			</view>
			<view class="right">
				<view class="cart btn u-line-1" @click="addCart()">加入购物车</view>
				<view class="buy btn u-line-1">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		shopDetail,
		addCart,
		CartList
	} from "../../common/http.api.js"
	export default {
		data() {
			return {
				shopdetail: {},
				commentLists: [],
				likegoods: [],
				list: [{
					name: '商品详情'
				}, {
					name: '商品评论',
					count: 0
				}, {
					name: '推荐商品',
				}],
				current: 0,
				collect: 0,
				id: '',
				count: 0
			}
		},
		async onLoad(option) {
			const detailList = await this.$u.get('/api/goods/' + option.id);
			if (uni.getStorageSync("storeState")) {
				this.getCartList()
			}
			this.shopdetail = detailList.data.goods;
			this.commentLists = detailList.data.goods.comments;
			this.likegoods = detailList.data.like_goods;
			this.list.forEach(item => {
				if (item.name == '商品评论') {
					item.count = this.commentLists.length;
				}
			});
			this.collect = detailList.data.goods.is_collect;
			this.id = detailList.data.goods.id
		},
		methods: {
			change(index) {
				this.current = index;
			},
			async addCart() {
				const res = await this.$u.api.addCart({
					goods_id: this.id
				});
				console.log(res);
				uni.showToast({
					title: '添加购物车成功',
					icon: "none"
				});
				// this.getCartList()
			},
			// 收藏
			async star() {
				const res = await this.$u.post('/api/collects/goods/' + this.id);
				console.log(res)
				if (this.collect) {
					this.collect = 0;
					uni.showToast({
						title: '取消收藏',
						icon: "none"
					})
				} else {
					this.collect = 1;
					uni.showToast({
						title: '收藏成功',
						icon: "none"
					})
				}

			},
			async getCartList() {
				const res = await this.$u.api.CartList();
				this.count = res.data.data.length;
			},
			toCart(){
				console.log(111)
				if(uni.getStorageSync("storeState")){
					uni.switchTab({
						url:"/pages/cart/cart"
					})
				}else{
					this.$u.utils.login()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding-bottom: 100rpx;

		.title {
			font-size: 36rpx;
			font-weight: bold;
			padding: 0rpx 40rpx;
			margin-bottom: 10rpx;
		}

		.goods-item {
			padding: 40rpx;
			box-shadow: 0 12rpx 20rpx 0 rgba(#000000, 0.1);
			margin-top: 30rpx;

			.title {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 28rpx;
			}

			.price {
				color: red;
			}

			.sales {
				color: #888;
			}
		}

		.center {
			display: flex;
			padding: 10rpx 40rpx;
			justify-content: space-between;

			.money {
				color: red;
				font-weight: bold;
				font-size: 36rpx;
			}

			.sales {
				font-size: 30rpx;
			}
		}

		.comment {
			display: flex;
			padding: 30rpx;

			.left {
				image {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
					background-color: #f2f2f2;
				}
			}

			.right {
				flex: 1;
				padding-left: 20rpx;
				font-size: 30rpx;

				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 10rpx;

					.name {
						color: #5677fc;
					}
				}

				.content {
					margin-bottom: 10rpx;
				}

				.date {
					color: #C0C0C0;
					font-size: 24rpx;
				}

			}
		}

		.navigation {
			width: 100%;
			display: flex;
			border: solid 2rpx #f2f2f2;
			background-color: #ffffff;
			height: 100rpx;
			align-items: center;
			position: fixed;
			text-align: center;
			justify-content: center;
			bottom: 0;

			.left {
				display: flex;
				font-size: 20rpx;

				.item {
					margin: 0 30rpx;

					&.car {
						text-align: center;
						position: relative;

						.car-num {
							position: absolute;
							top: -10rpx;
							right: -10rpx;
						}
					}
				}
			}

			.right {
				display: flex;
				font-size: 28rpx;
				align-items: center;

				.btn {
					line-height: 66rpx;
					padding: 0 30rpx;
					border-radius: 36rpx;
					color: #ffffff;
				}

				.cart {
					background-color: #ed3f14;
					margin-right: 30rpx;
				}

				.buy {
					background-color: #ff7900;
				}
			}
		}
	}
</style>
